import { getUserInfo } from "../../apis/auth.js";
import { register, login } from "../../apis/auth.js";

export const user = {
  state() {
    return {
      user:
        {
          username: "张五常",
          name: "张五常昵称",
          intro: "我是张五常的简介",
          mobilePhone: "13512345678",
          gender: "M",
          webSite: "www.iamZWC.com",
        } || getUserInfo(),
      userAvatar: "",
    };
  },
  mutations: {
    changeUserInfo(state, val) {
      state.user = val;
    },
    changeUserAvatar(state, val) {
      state.userAvatar = val;
    },
  },
  actions: {
    async registerUser(context, { email, username, password }) {
      const res = await register(email, username, password);
      context.commit("changeUserInfo", res);
    },
    async loginUser(context, { email, password }) {
      const res = await login(email, password);
      context.commit("changeUserInfo", res);
    },
    async ChangeUserInfoAsync(context, val) {
      return new Promise((resolve) => {
        setTimeout(() => {
          context.commit("changeUserInfo", val);
          resolve();
        }, 500);
      });
    },
  },
};
